/**
 * @file   1056.cpp
 * @author Shuang Hu <hsmath@ubuntu>
 * @date   Tue Jul 27 22:49:41 2021
 * 
 * @brief  PAT advanced 1056:'shell-sort style'
 * 
 * 
 */
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
vector<int> mice;
class Solution{
private:
    int Np;
    int Ng;
    vector<int> rank;
    vector<int> seq;
    vector<int> temp;//Store the winner of each tern;
    map<int,vector<int>> roundpeople;//mark which person enter round 'd'?
    int totalround;
public:
    Solution();
    void solve();
    void setrank();
    int findmaxindex(int front,int rear);//Find the best one from a group
    void print();
};
bool cmp(int a,int b){
    return mice[a]>mice[b];
}
Solution::Solution(){
    cin>>Np>>Ng;
    for(int i=0;i<Np;i++){
	int d;
	cin>>d;
	mice.push_back(d);
    }
    rank.resize(Np);
    for(int i=0;i<Np;i++){
	int d;
	cin>>d;
	seq.push_back(d);
    }
    totalround=0;
    temp=seq;
}
void Solution::solve(){
    while(temp.size()>Ng){
	vector<int> winner;
	for(int i=0;i<temp.size();i=i+Ng){
	    int d=i+Ng;
	    int d2=temp.size();
	    int rear=min(d,d2);
	    int index=findmaxindex(i,rear);
	    winner.push_back(temp[index]);
	}
	temp=winner;
	totalround++;
    }
    sort(temp.begin(),temp.end(),cmp);
    totalround--;
    setrank();
}
int Solution::findmaxindex(int front,int rear){
    int index=front;
    for(int i=front;i<rear;i++){
	if(mice[temp[i]]>mice[temp[index]]){
	    index=i;
	}
    }
    for(int i=front;i<rear;i++){
	if(i!=index){
	    roundpeople[totalround].push_back(temp[i]);
	}
    }
    return index;
}
void Solution::setrank(){
    for(int i=0;i<temp.size();i++){
	rank[temp[i]]=i+1;//the winners!
    }
    int nowrank=temp.size()+1;
    for(int i=totalround;i>=0;i--){
	for(int j=0;j<roundpeople[i].size();j++){
	    rank[roundpeople[i][j]]=nowrank;
	}
	nowrank=nowrank+roundpeople[i].size();
    }
}
void Solution::print(){
    cout<<rank[0];
    for(int i=1;i<Np;i++){
	cout<<" "<<rank[i];
    }
    cout<<endl;
}
int main(){
    Solution S;
    S.solve();
    S.print();
}
